﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Sec;
using ESOL.Common;
using ESOL.Common.Entity;
using DevExpress.XtraTreeList.Nodes;

namespace ESOL.WinUI.Sec
{
    public partial class frmMaintainMenuRole : BaseForm.frmBaseDialogyMaintainForm
    {
        #region Variable
        public SecMenuEntity MenuEntity { get; set; }
        #endregion

        #region Constructor
        public frmMaintainMenuRole()
        {
            InitializeComponent();
            ThisActionType = ActionType.GrandRole;
        }
        #endregion

        #region Functions
        private void BindCmbParentMenu()
        {
            List<SecMenuEntity> list = SecMenuBs.GetAllMenus();
            cmbParentMenu.DataSource = list;
            SecMenuEntity menu = new SecMenuEntity();
            list.Insert(0, menu);
            cmbParentMenu.DisplayMember = "MenuName";
            cmbParentMenu.ValueMember = "MenuId";
        }

        private void BindTreeRole()
        {
            List<SecRoleEntity> list = SecRoleBs.GetRolesAllList();
            treeRoles.DataSource = list;
            treeRoles.ExpandAll();
        }

        private void GetRoleMenuListFromTree(TreeListNodes nodes, List<SecRoleMenuEntity> list)
        {
            if (MenuEntity == null)
            {
                return;
            }
            if (list == null)
            {
                list = new List<SecRoleMenuEntity>();
            }
            if (nodes == null || nodes.Count == 0)
            {
                return;
            }
            foreach (TreeListNode node in nodes)
            {
                if (node.Checked == false)
                {
                    continue;
                }
                SecRoleMenuEntity entity = new SecRoleMenuEntity();
                entity.MenuId = MenuEntity.MenuId;
                entity.RoleId = node.GetValue("RoleId").ToString();
                list.Add(entity);
                GetRoleMenuListFromTree(node.Nodes, list);
            }
        }

        private bool SaveMenuRole()
        {
            if(MenuEntity == null)
            {
                return false;
            }
            try
            {
                List<SecRoleMenuEntity> list = new List<SecRoleMenuEntity>();
                GetRoleMenuListFromTree(treeRoles.Nodes, list);
                bool isSuccessful = SecRoleMenuBs.SaveRoleMenu(list, MenuEntity.MenuId);
                return isSuccessful;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message,"",MessageBoxButtons.OK,MessageBoxIcon.Information);
                return false;
            }
        }

        protected override void Click_Save()
        {
            if (SaveMenuRole())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void SetFields()
        {
            if (MenuEntity == null)
            {
                return;
            }
            txtMenuName.Text = MenuEntity.MenuName;
            if (MenuEntity.ParentMenuEntity == null)
            {
                MenuEntity.ParentMenuEntity = SecMenuBs.GetMenuByID(MenuEntity.ParentMenu);
            }
            if (MenuEntity.ParentMenuEntity == null)
            {
                cmbParentMenu.SelectedIndex = -1;
            }
            else
            {
                cmbParentMenu.SetSelectedValue(MenuEntity.ParentMenuEntity.MenuId);
            }
            List<SecRoleMenuEntity> list = SecRoleMenuBs.GetRoleMenusByMenuID(MenuEntity.MenuId);
            SetTreeRoleChecked(list, treeRoles.Nodes);
        }

        private void SetTreeRoleChecked(List<SecRoleMenuEntity> list, TreeListNodes nodes)
        {
            if (list == null || nodes == null || nodes.Count == 0)
            {
                return;
            }
            foreach (TreeListNode node in nodes)
            {
                if (IsRoleInList(node.GetValue("RoleId").ToString(), list))
                {
                    node.Checked = true;
                }
                else
                {
                    node.Checked = false;
                }
                SetTreeRoleChecked(list, node.Nodes);
            }
        }

        private bool IsRoleInList(string roleID, List<SecRoleMenuEntity> list)
        {
            if (list == null)
            {
                return false;
            }
            foreach (SecRoleMenuEntity entity in list)
            {
                if (entity.RoleId == roleID)
                {
                    return true;
                }
            }
            return false;
        }
        #endregion

        #region Event
        private void frmMaintainMenuRole_Load(object sender, EventArgs e)
        {
            BindCmbParentMenu();
            BindTreeRole();
            SetFields();
        }
        #endregion
    }
}
